﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Jalal.Library.Utility;
using Jalal.Library.DataStructure;
using Jalal.Library.Data;
using BCGC.DAL;

namespace BCGC.BLL.BusinessObject
{
    public class Game
    {
        private int gameId;
        public int GameId
        {
            get { return gameId; }
            private set { gameId = value; }
        }

        private string gameTitle;
        public string GameTitle
        {
            get { return gameTitle; }
            set { gameTitle = value; }
        }

        private string gameIconFileUrl;
        public string GameIconFileUrl
        {
            get { return gameIconFileUrl; }
            set { gameIconFileUrl = value; }
        }

        private string gameImageFileUrl;
        public string GameImageFileUrl
        {
            get { return gameImageFileUrl; }
            set { gameImageFileUrl = value; }
        }

        private int crackCount;
        public int CrackCount
        {
            get { return crackCount; }
            private set { crackCount = value; }
        }

        private int patchCount;
        public int PatchCount
        {
            get { return patchCount; }
            private set { patchCount = value; }
        }

        private int cheatCodeCount;
        public int CheatCodeCount
        {
            get { return cheatCodeCount; }
            private set { cheatCodeCount = value; }
        }

        private int trialCount;
        public int TrialCount
        {
            get { return trialCount; }
            private set { trialCount = value; }
        }

        private int trailerCount;
        public int TrailerCount
        {
            get { return trailerCount; }
            private set { trailerCount = value; }
        }

        private int wallpaperCount;
        public int WallpaperCount
        {
            get { return wallpaperCount; }
            private set { wallpaperCount = value; }
        }

        private int otherResourceCount;
        public int OtherResourceCount
        {
            get { return otherResourceCount; }
            private set { otherResourceCount = value; }
        }

        public Game()
        {
            this.gameId = 0;
            this.gameTitle = string.Empty;
            this.gameIconFileUrl = string.Empty;
            this.gameImageFileUrl = string.Empty;
            this.crackCount = 0;
            this.patchCount = 0;
            this.cheatCodeCount = 0;
            this.wallpaperCount = 0;
            this.trailerCount = 0;
            this.trialCount = 0;
            this.otherResourceCount = 0;
        }

        public Game(DataRow dataRow)
        {
            if (dataRow != null)
            {
                if (dataRow.Table.Columns.Contains("GameId"))
                    this.gameId = (int)CommonUtility.FilterNull(dataRow["GameId"], typeof(int));
                if (dataRow.Table.Columns.Contains("GameTitle"))
                    this.gameTitle = (string)CommonUtility.FilterNull(dataRow["GameTitle"], typeof(string));
                if (dataRow.Table.Columns.Contains("GameIconFileUrl"))
                    this.gameIconFileUrl = (string)CommonUtility.FilterNull(dataRow["GameIconFileUrl"], typeof(string));
                if (dataRow.Table.Columns.Contains("GameImageFileUrl"))
                    this.gameImageFileUrl = (string)CommonUtility.FilterNull(dataRow["GameImageFileUrl"], typeof(string));
                if (dataRow.Table.Columns.Contains("CrackCount"))
                    this.crackCount = (int)CommonUtility.FilterNull(dataRow["CrackCount"], typeof(int));
                if (dataRow.Table.Columns.Contains("PatchCount"))
                    this.patchCount = (int)CommonUtility.FilterNull(dataRow["PatchCount"], typeof(int));
                if (dataRow.Table.Columns.Contains("CheatCodeCount"))
                    this.cheatCodeCount = (int)CommonUtility.FilterNull(dataRow["CheatCodeCount"], typeof(int));
                if (dataRow.Table.Columns.Contains("WallpaperCount"))
                    this.wallpaperCount = (int)CommonUtility.FilterNull(dataRow["WallpaperCount"], typeof(int));
                if (dataRow.Table.Columns.Contains("TrailerCount"))
                    this.trailerCount = (int)CommonUtility.FilterNull(dataRow["TrailerCount"], typeof(int));
                if (dataRow.Table.Columns.Contains("TrialCount"))
                    this.trialCount = (int)CommonUtility.FilterNull(dataRow["TrialCount"], typeof(int));
                if (dataRow.Table.Columns.Contains("OtherResourceCount"))
                    this.otherResourceCount = (int)CommonUtility.FilterNull(dataRow["OtherResourceCount"], typeof(int));
            }
            else
            {
                this.gameId = 0;
                this.gameTitle = string.Empty;
                this.gameIconFileUrl = string.Empty;
                this.gameImageFileUrl = string.Empty;
                this.crackCount = 0;
                this.patchCount = 0;
                this.cheatCodeCount = 0;
                this.wallpaperCount = 0;
                this.trailerCount = 0;
                this.trialCount = 0;
                this.otherResourceCount = 0;
            }
        }

        private static ReadWriteCustomCollection<Game> LoadFromDataTable(DataTable data)
        {
            ReadWriteCustomCollection<Game> result = new ReadWriteCustomCollection<Game>();

            if (data != null && data.Rows.Count > 0)
            {
                foreach (DataRow row in data.Rows)
                {
                    result.Add(new Game(row));
                }
            }
            return result;
        }

        public static ReadWriteCustomCollection<Game> GetAllGames()
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            ReadWriteCustomCollection<Game> games = LoadFromDataTable(GameData.GetAllGames());
            DataFactory.GetDataMiner().CommitTransaction();
            return games;
        }

        public static bool Create(string gameName)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            int id = GameData.CreateNewGame(gameName);
            DataFactory.GetDataMiner().CommitTransaction();

            return (id > 0);
        }

        public static ReadWriteCustomCollection<Game> GetAllGames(int currentPageIndex, string sortOrder, 
            int pageSize, string title, out int totalData)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);

            totalData = 0;

            DataTable gameData = GameData.GetAllGames(currentPageIndex, sortOrder, pageSize, title, out totalData);
            ReadWriteCustomCollection<Game> games = LoadFromDataTable(gameData);

            DataFactory.GetDataMiner().CommitTransaction();

            return games;
        }
    }
}
